import random

import requests
from config import HOST


class ApiRegisterLogin:
    def __init__(self, session):
        self.session = session
        # 图片验证码url
        self.__url_img_code = HOST + "/common/public/verifycode1/{}"
        # 短信验证码url
        self.__url_phone_code = HOST + "/member/public/sendSms"
        # 注册url
        self.__url_register = HOST + "/member/public/reg"
        # 登录url
        self.__url_login = HOST + "/member/public/login"
        # 登录状态url
        self.__url_login_status = HOST + "/member/public/islogin"
        # 退出登录
        self.__logout = HOST + "/member/member/logout"

    # 1、获取图片验证码接口 封装
    def api_img_code(self, random):
        # 调用get方法 返回响应对象
        return self.session.get(url=self.__url_img_code.format(random))

    # 2、获取短信验证码接口 封装
    def api_phone_code(self, phone, imgVerifyCode):
        # 1、定义请求参数
        data = {
            "phone": phone,
            "imgVerifyCode": imgVerifyCode,
            "type": "reg"
        }
        # 2、调用请求方法
        return self.session.post(url=self.__url_phone_code, data=data)

    # 3、注册接口 封装
    def api_register(self, phone, password, imgVerifyCode, phone_code):
        data = data = {
            "phone": phone,
            "password": password,
            "verifycode": imgVerifyCode,
            "phone_code": phone_code,
            "dy_server": "on"
        }
        return self.session.post(url=self.__url_register, data=data)

    # 4、登录接口 封装
    def api_login(self, phone, password):
        data = {"keywords": phone, "password": password}
        return self.session.post(url=self.__url_login, data=data)

    # 5、登录状态 封装
    def api_login_status(self):
        return self.session.post(url=self.__url_login_status)

    def logout(self):
        return self.session.get(url=self.__logout)
